﻿Imports TodoMicroSolutionPackV1.entidades.dal.dto
Imports TodoMicroSolutionPackV1.dal.dao
Imports TodoMicroSolutionPackV1.dal.dto.ubicacion
Imports TodoMicroSolutionPackV1.dal.dto
Imports NHibernate
Imports NHibernate.Cfg
Public Class frmABMEntidades
    Private ent As entidad = New entidad
    Private _session As ISession
    Private DAOFactory As NHibernatedaofactory

    Protected ReadOnly Property Session As ISession
        Get
            If _session Is Nothing Then
                _session = NhibernateSessionFactory.getSession
            End If
            Return _session
        End Get
    End Property

    Private Sub frmABMEntidades_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        If Not _session Is Nothing Then
            _session.Dispose()
        End If
    End Sub
    Private Sub frmABMEntidades_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DAOFactory = New NhibernateDAOFactory(Session)
        comunes.populateCombobox(DAOFactory.getProvinciaDAO.FindAll, "nombre", "id", cmbProvincia)
        comunes.populateCombobox(DAOFactory.getConstanteDAO.getConstantes("facturacion", "documento"), "valor", "codigo", cmbTipodocumento)

        Dim condicion_iva As IList = DAOFactory.getConstanteDAO.getConstantes("facturacion", "condicion_iva_b")
        For Each item In DAOFactory.getConstanteDAO.getConstantes("facturacion", "condicion_iva_a")
            condicion_iva.Add(item)
        Next

        comunes.populateCombobox(condicion_iva, "valor", "codigo", cmbPosicionIva)
    End Sub

    Private Sub btnEntidadExistente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEntidadExistente.Click
        ent = comunes.getCliente(Session)
        If Not ent Is Nothing Then
            entidadToFrm()
        End If
    End Sub
    Private Sub entidadToFrm()
        txtId.Text = ent.id
        txtRazonSocial.Text = ent.razon_social
        txtDireccion.Text = ent.domicilio
        cmbProvincia.SelectedIndex = cmbProvincia.FindStringExact(ent.localidad.departamento.provincia.nombre)
        cmbLocalidad.SelectedIndex = cmbLocalidad.FindStringExact(ent.localidad.nombreAMostrar)
        cmbTipodocumento.SelectedIndex = cmbTipodocumento.FindStringExact(ent.tipo_documento.valor)
        txtNumeroDucumento.Text = ent.numero_documento
        cmbPosicionIva.SelectedIndex = cmbPosicionIva.FindStringExact(ent.posicion_iva.valor)
        cbCliente.Checked = ent.esCliente
        cbProveedor.Checked = ent.esProveedor
        txtNombre.Text = ent.nombre
        txtApellido.Text = ent.apellido
        txtEmail1.Text = ent.email1
        txtEmail2.Text = ent.email2
        txtEmail3.Text = ent.email3
        txtTel1.Text = ent.telefono1
        txtTel2.Text = ent.telefono2
        txtTel3.Text = ent.telefono3
    End Sub
    Private Sub frmToEntidad()
        ent.razon_social = txtRazonSocial.Text
        ent.domicilio = txtDireccion.Text
        ent.localidad = cmbLocalidad.SelectedItem
        ent.tipo_documento = cmbTipodocumento.SelectedItem
        ent.numero_documento = Convert.ToInt64(txtNumeroDucumento.Text)
        ent.posicion_iva = cmbPosicionIva.SelectedItem
        If cbCliente.Checked And cbProveedor.Checked Then
            ent.tipo_entidad = DAOFactory.getConstanteDAO.getTipoEntidad("11")
        ElseIf cbProveedor.Checked Then
            ent.tipo_entidad = DAOFactory.getConstanteDAO.getTipoEntidad("10")
        ElseIf cbCliente.Checked Then
            ent.tipo_entidad = DAOFactory.getConstanteDAO.getTipoEntidad("01")
        Else
            ent.tipo_entidad = Nothing
        End If
        ent.nombre = txtNombre.Text
        ent.apellido = txtApellido.Text
        ent.email1 = txtEmail1.Text
        ent.email2 = txtEmail2.Text
        ent.email3 = txtEmail3.Text
        ent.telefono1 = txtTel1.Text
        ent.telefono2 = txtTel2.Text
        ent.telefono3 = txtTel3.Text

    End Sub
    Private Sub borrarFrm()
        txtId.Text = ""
        txtRazonSocial.Text = ""
        txtDireccion.Text = ""
        txtNumeroDucumento.Text = ""
        cbCliente.Checked = False
        cbProveedor.Checked = False
        txtNombre.Text = ""
        txtApellido.Text = ""
        txtEmail1.Text = ""
        txtEmail2.Text = ""
        txtEmail3.Text = ""
        txtTel1.Text = ""
        txtTel2.Text = ""
        txtTel3.Text = ""
    End Sub
    Private Sub cmbProvincia_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbProvincia.SelectedIndexChanged
        comunes.populateCombobox(DAOFactory.getLocalidadDAO.getLocalidadesPorProvincia(cmbProvincia.SelectedItem), "nombreAMostrar", "id", cmbLocalidad)
    End Sub

    Private Sub btnNuevaEntidad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevaEntidad.Click
        ent = New entidad
        borrarFrm()
    End Sub

    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        frmToEntidad()
        Using tx As ITransaction = Session.BeginTransaction
            DAOFactory.getEntidadDAO.SaveOrUpdate(ent)
            tx.Commit()
        End Using
        txtId.Text = ent.id
        MsgBox("Entidad guardada con exito", MsgBoxStyle.Information, "TodoMicroSolutionPack")
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

End Class